<?php
$loader->requireOnce('includes/ResultParser/IParserEngine.interface.php');
$loader->requireOnce('lib/DICOM/DICOM.php');
class DICOMParse implements IParserEngine {
    private     $logger;
    function __construct(){
         $this->logger = LoggerManager::getLogger('core.include.ResultParser.DICOMParser');
    }
    public function parse($uploadedFile, $fullFileName, $thumbnailFileName, $ext, $thumbnailWidth, $thumbnailHeight,  $proportion = true){
        $this->logger->info("Start function parse {fullFileName='$fullFileName', thumbnailFileName='$thumbnailFileName'}");
        clearstatcache();
        $orgFileName    =   $uploadedFile['name'];
        $fullDir        =   substr($fullFileName, 0, strrpos($fullFileName,"/") + 1);
        $fileName       =   APP_ROOT . $fullDir . $orgFileName;
        // Move uploaded file to DICOM folder on server
        $this->logger->debug("Move dicom uploaded file to dicom folder {tmpFile=" . $uploadedFile['tmp_name'] . ", targetFile=$fileName}");
        move_uploaded_file($uploadedFile['tmp_name'], $fileName);
		$pgmFile		=	substr($fileName, 0, strrpos($fullFileName,".") + 1);
        $dicom  =   new File_DICOM();
        $res    =   $dicom->parse($fileName);
        $res    =   $dicom->dumpImage($pgmFile. ".pgm");
        if (PEAR::isError($res)) {
        	$this->logger->error("Parsing DICOM file failed");
        	throw new Exception("Parsing DICOM file failed");
        }
       
        /*$images =   $dicom->imagecreatefromDICOM($fileName);
        $this->logger->debug("image numbers=" . count($images));
        if (count($images) == 1) {
        	$number	=	0;
        }
        else {
        	$number	=	1;
        }
        foreach ($images as $im){
	        $this->logger->debug("Start creating thumbnail image resource from full image resource");
	        $x = imagesx($im);
	        $y = imagesy($im);
	        if($x <= $thumbnailWidth && $y <= $thumbnailHeight) {
	            $im2   =   $im;
	        }
	        else {
	            if (!$proportion) {
	                if($x >= $y) {
	                    $newx = $thumbnailWidth;
	                    $newy = $newx * $y / $x;
	                } else {
	                    $newy = $thumbnailHeight;
	                    $newx = $x / $y * $newy;
	                }
	            }
	            else {
	                $ratio  =   min(array($thumbnailWidth/$x,$thumbnailHeight/$y));
	                $newx   =   $ratio*$x;
	                $newy   =   $ratio*$y;
	            }
	            $im2 = imagecreatetruecolor($newx, $newy);
	            imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
	        }
	        
	        // Create thumbnail file
	        $thumbnailFileName	=	APP_ROOT . $thumbnailFileName . "_" . $number . ".jpeg";
	        imagejpeg($im2, $thumbnailFileName);
	        imagedestroy($im2);
	        $fullFileName	=	APP_ROOT . $fullFileName . "_" . $number . ".jpeg";
	        imagejpeg($im, $fullFileName);
	        imagedestroy($im);
	        $number++;
        }*/
        $this->logger->info("Finish function parse {fullFileName='$fullFileName', thumbnailFileName='$thumbnailFileName'}");
    }
}
?>